package com.android.gilastv.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.gilastv.R;
import com.android.gilastv.bean.MenuInfo;

import java.util.List;

/**
 * Created by Administrator on 2018/8/22.
 */

public class MenuListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    List<MenuInfo> list;
    CommCallBack callBack;
     int select = 0;

    public MenuListAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setCallBack(CommCallBack callBack) {
        this.callBack = callBack;
    }

    public void setData(List<MenuInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public MenuInfo getSelect()
    {
        return list.get(select);
    }

    public void setSelect(int select)
    {
        this.select= select;
        if (callBack != null&& select!=-1) {
            callBack.onResult(list.get(select));
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_homemenu, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        if (select == position) {
            viewHolder.itemView.setBackgroundColor(mContext.getResources().getColor(R.color.mainColor));
        } else {
            viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
        }

        if(position>=0) {
            final MenuInfo info = list.get(position);
            //数据绑定逻辑
            viewHolder.tv_text.setText(info.dataLabel);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    select = position;
                    notifyDataSetChanged();
                    if (callBack != null) {
                        callBack.onResult(info);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_text;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_text = itemView.findViewById(R.id.tv_text);
        }
    }
}
